package com.apex.aolonfitdemo.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;


import com.apex.aolonfit.AolonFitManager;
import com.apex.aolonfitdemo.R;
import com.apex.aolonfitdemo.databinding.ActivityMsgBinding;
import com.apex.aolonfitdemo.dialog.WaitingDialog;
import com.apex.ax_bluetooth.callback.GeneralCallback;
import com.apex.ax_bluetooth.callback.MsgContentCallback;
import com.apex.ax_bluetooth.core.EABleManager;
import com.apex.ax_bluetooth.enumeration.EABleConnectState;
import com.apex.ax_bluetooth.enumeration.QueryWatchInfoType;
import com.cyber.bridge.module.bean.QuickReplyEntity;
import com.cyber.bridge.module.listener.OnCyberQuickReplyListener;

import java.util.ArrayList;
import java.util.List;


public class MsgActivity extends AppCompatActivity {
    private final String TAG = this.getClass().getSimpleName();
    private WaitingDialog waitingDialog;
    int maxCount;

    List<QuickReplyEntity> msgList;
    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0x42) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                activityMsgBinding.submit.setEnabled(true);
            } else if (msg.what == 0x43) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                activityMsgBinding.submit.setEnabled(true);
                Toast.makeText(MsgActivity.this, getString(R.string.modification_failed), Toast.LENGTH_SHORT).show();
            }
        }
    };
    ActivityMsgBinding activityMsgBinding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityMsgBinding = ActivityMsgBinding.inflate(getLayoutInflater());
        setContentView(activityMsgBinding.getRoot());
        activityMsgBinding.tool.setNavigationIcon(R.mipmap.exit_page);
        setSupportActionBar(activityMsgBinding.tool);
        getSupportActionBar().setTitle(null);
        activityMsgBinding.tool.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        AolonFitManager.getInstance().setOnQuickReplyListener(new OnCyberQuickReplyListener() {
            @Override
            public void setQuickReplySwitch(boolean b) {

            }

            @Override
            public void sendQuickReplyList(boolean b) {
                if (mHandler != null) {
                    mHandler.sendEmptyMessage(b ? 0x42 : 0x43);
                }

            }

            @Override
            public void queryQuickReplyMaxCount(int i) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                maxCount = i;
            }
        });
        if (waitingDialog == null) {
            waitingDialog = new WaitingDialog(MsgActivity.this);
        }
        AolonFitManager.getInstance().queryQuickReplyMaxCount();
        activityMsgBinding.submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activityMsgBinding.submit.setEnabled(false);
                String msg1 = activityMsgBinding.msg1.getText().toString();
                String msg2 = activityMsgBinding.msg2.getText().toString();
                String msg3 = activityMsgBinding.msg3.getText().toString();
                String msg4 = activityMsgBinding.msg4.getText().toString();
                if (TextUtils.isEmpty(msg1) && TextUtils.isEmpty(msg2) && TextUtils.isEmpty(msg3) && TextUtils.isEmpty(msg4)) {
                    activityMsgBinding.submit.setEnabled(true);
                    return;
                }
                if (msgList == null) {
                    msgList = new ArrayList<>();
                }
                msgList.clear();
                if (!TextUtils.isEmpty(msg1)) {
                    msgList.add(new QuickReplyEntity(msg1));
                }
                if (!TextUtils.isEmpty(msg2)) {
                    msgList.add(new QuickReplyEntity(msg2));
                }
                if (!TextUtils.isEmpty(msg3)) {
                    msgList.add(new QuickReplyEntity(msg3));
                }
                if (!TextUtils.isEmpty(msg4)) {
                    msgList.add(new QuickReplyEntity(msg4));
                }
                AolonFitManager.getInstance().sendQuickReplyList(msgList);
            }
        });
    }

    @Override
    protected void onDestroy() {

        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
            mHandler = null;
        }
        if (waitingDialog != null) {
            waitingDialog.dismiss();
            waitingDialog = null;
        }

        super.onDestroy();
    }
}
